<?php

global $_W, $_GPC;
include 'function.php';

$ops = array('list', 'talk_post', 'talk_delete', 'talk_pinglun', 'pic_delete', 'pinglun_del');
$op  = in_array($_GPC['op'], $ops) ? $_GPC['op'] : 'list';

if ($op == 'list') {
    $type = isset($_GPC['type']) ? $_GPC['type'] : "0";

    $pageIndex = max(1, intval($_GPC['page']));
    $pageSize  = 10;

    $keywords = safe_gpc_string($_GPC['talk_content']);

    $sql   = "SELECT * FROM " . tablename('dgjy_talk');
    $where = " WHERE uniacid=:uniacid ";

    //筛选官方话题和其他话题
    if (!empty($type)) {
        $where = $where . " AND talk_type = " . $type;
    }

    //关键字查找
    if (!empty($keywords)) {
        $where = $where . " AND talk_content LIKE '%" . $keywords . "%' ";
    }

    $total      = pdo_fetchcolumn("SELECT count(*) FROM" . tablename('dgjy_talk') . $where, array(':uniacid' => $_W['uniacid']));
    $pagination = pagination($total, $pageIndex, $pageSize);

    $sql = $sql . $where . " ORDER BY talk_did DESC, talk_id DESC LIMIT " . ($pageIndex - 1) * $pageSize . "," . $pageSize;
//    var_dump($sql);
    $talk = pdo_fetchall($sql, array(':uniacid' => $_W['uniacid']));

    include $this->template('talk/talk');
}

if ($op == 'talk_post') {
    $_W['page']['title'] = '编辑官方话题';

    $query   = load()->object('query');
    $talk_id = intval($_GPC['talk_id']);
    $talk    = $query->from('dgjy_talk')->where('talk_id', $talk_id)->get();//根据id查找是否存在该话题
//    var_dump($talk);
    $isnew = false;
    if (empty($talk)) {//不存在则新建
        $isnew = true;
        $talk  = array(
            'talk_did' => 1,
        );
    } else {//存在则查询其中的图片
        $picture = pdo_fetchall("SELECT * FROM" . tablename('dgjy_picture') . "WHERE uniacid=:uniacid AND picture_vid=:picture_vid AND picture_type=:picture_type AND picture_pid=:picture_pid",
            array(':uniacid' => $_W['uniacid'], ':picture_vid' => $_GPC['talk_id'], ':picture_type' => 'talk', ':picture_pid' => '1'));
    }

    if (checksubmit('submit')) {
        $picture = $_GPC['picture'];
        if (count($picture) > 3) {
            itoast('最多上传三张图片', referer(), 'error');
            return;
        }
        $data = array(
            'uniacid'      => $_W['uniacid'],
            'talk_pid'     => "4",
            'talk_type'    => "1",//官方话题
            'talk_did'     => $_GPC['talk_did'],
            'talk_title'   => $_GPC['talk_title'],
            'talk_content' => $_GPC['talk_content'],

            'talk_time' => $_W['timestamp'],
        );
        if ($isnew) {//新的,新增
            $res = pdo_insert('dgjy_talk', $data);
        } else {//否则更新
            $res = pdo_update('dgjy_talk', $data, array('talk_id' => $talk_id));
        }
        if (!empty($res)) {

            if ($isnew) {
                $vid = pdo_insertId();//新建话题
            } else {
                $vid = $talk_id;//添加的图片为当前话题的图片
            }

            //上传图片开始
            foreach ($picture as $key => $v) {
                $picdata = array(
                    'uniacid'           => $_W['uniacid'],
                    'picture_pid'       => '1',
                    'picture_type'      => 'talk',
                    'picture_vid'       => $vid,
                    'picture_show'      => '0',
                    'openid'            => $_GPC['openid'],
                    'picture_url'       => $v,
                    'picture_time'      => $_W['timestamp'],
                    'picture_ip'        => $_W['clientip'],
                    'picture_container' => $_W['container'],
                    'picture_os'        => $_W['os'],
                );

                pdo_insert('dgjy_picture', $picdata);

            }
            //上传图片结束
            message($isnew ? '恭喜，发布成功！' : '恭喜，编辑成功！', $this->createWebUrl('talk', array('op' => 'list', 'type' => '1')), 'success');
        } else {
            message($isnew ? '抱歉，发布失败！' : '抱歉，编辑失败！', referer(), 'error');
        }
    }

    include $this->template('talk/talk_add');
}

if ($op == 'talk_delete') {
    $id    = intval($_GPC['talk_id']);
    $query = load()->object('query');
    $talk  = $query->from('dgjy_talk')->where('talk_id', $id)->get();//根据id查找是否存在该话题
    pdo_delete('dgjy_talk', array('talk_id' => $id));//删除话题
    pdo_delete('dgjy_pinglun', array('pinglun_type' => 'talk', 'pinglun_vid' => $id));//删除评论
    if ($talk['talk_type'] == 2) {//用户话题,删除服务器图片文件
        $pictures = $query->from('dgjy_picture')->where('picture_vid', $id)->getall();//获取所有图片
        foreach ($pictures as $value) {
            load()->func('file');
            file_delete($value['picture_url']);//删除本地文件
        }
    }
    pdo_delete('dgjy_picture', array('picture_type' => 'talk', 'picture_vid' => $id));//删除数据库图片数据记录

    itoast('删除话题成功', referer(), 'success');
}

if ($op == 'pic_delete') {//只有官方话题可以编辑,所以可以不用考虑话题中图片的删除问题,如何用户话题可以编辑,则需要在服务器上删除用户上传的图片
    if (!empty($_GPC['picture_id'])) {
        $picture = pdo_fetch("SELECT * FROM" . tablename('dgjy_picture') . "WHERE uniacid=:uniacid AND picture_id=:picture_id", array(':uniacid' => $_W['uniacid'], ':picture_id' => $_GPC['picture_id']));
        if (!empty($picture)) {
            if ($picture['uniacid'] == $_W['uniacid']) {
                $res = pdo_delete('dgjy_picture', array('uniacid' => $_W['uniacid'], 'picture_id' => $_GPC['picture_id']));
                if (!empty($res)) {
                    itoast('恭喜！删除成功！', referer(), 'success');
                } else {
                    itoast('抱歉！删除失败！', referer(), 'error');
                }

            } else {
                itoast('抱歉！您没有权限删除此信息！', referer(), 'error');
            }
        } else {
            itoast('抱歉！您要删除的信息不存在！', referer(), 'error');
        }
    } else {
        itoast('警告！请勿提交非法网址！', referer(), 'error');
    }
}

if ($op == 'talk_pinglun') {
    $talk_id    = intval($_GPC['talk_id']);
    $pageIndex  = max(1, intval($_GPC['page']));
    $pageSize   = 15;
    $total      = pdo_fetchcolumn("SELECT count(*) FROM " . tablename('dgjy_pinglun') . " WHERE uniacid=:uniacid", array(':uniacid' => $_W['uniacid']));
    $pagination = pagination($total, $pageIndex, $pageSize);
    $pinglun    = pdo_fetchall("SELECT * FROM" . tablename('dgjy_pinglun') . "WHERE uniacid=:uniacid AND pinglun_vid=:pinglun_vid ORDER BY pinglun_id DESC LIMIT " . ($pageIndex - 1) * $pageSize . "," . $pageSize,
        array(':uniacid' => $_W['uniacid'], ':pinglun_vid' => $talk_id));

    include $this->template('talk/talk_pinglun');
}

if ($op == 'pinglun_del') {
    $id = intval($_GPC['pinglun_id']);
    pdo_delete('dgjy_pinglun', array('pinglun_id' => $id));
    itoast('评论删除成功', referer(), 'success');
}

?>